//
//  MYGMedicalToolsSheetChooseView.swift
//  MYDoctor
//
//  Created by 林矗 on 2024/5/27.
//

import UIKit
import QMUIKit
class MYGMedicalToolsSheetChooseView: UIView {

    var title: String = "" {
        didSet {
            titleLabel.text = title
        }
    }
    
    var content: String = "" {
        didSet {
            dropDownButton.setTitle("\(content) ", for: .normal)
        }
    }
    
    var items: [String] = []
    
    var action: ((Int)->())?
    
    
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI
    private func configUI() {
        addSubviews([
            titleLabel,
            dropDownButton
        ])
        
        titleLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(0)
        }
        
        dropDownButton.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-0)
            make.width.greaterThanOrEqualTo(100)
            make.top.bottom.equalToSuperview()
        }
    }
    
    
    // MARK: - Public funcs
    
    // MARK: - lazy load
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(16)
        label.textColor = .rgba(51, 51, 51)
        label.text = "性别"
        return label
    }()
    
    private lazy var dropDownButton: QMUIButton = {
        let button = QMUIButton(type: .custom)
        button.imagePosition = .right
        button.setImage(UIImage(named: "Community/dropdown"), for: .normal)
        button.titleLabel?.font = .medium(16)
        button.setTitleColor(.rgba(32, 32, 32), for: .normal)
        button.addTarget(self, action: #selector(dropDown), for: .touchUpInside)
        button.contentHorizontalAlignment = .right
        button.spacingBetweenImageAndTitle = 4
        return button
    }()
}

extension MYGMedicalToolsSheetChooseView {
    @objc func dropDown() {
        let view = MYGChooseListView()
        view.reload(with: items)
        let sheet = MYGBottomSheetView(with: view)
        sheet.title = "请选择\(title)"
        sheet.show(animated: true)
        view.didSelectItemAction = { (index, item) in
            sheet.hide(animated: true)
            self.dropDownButton.setTitle(item.getTitle(), for: .normal)
            self.action?(index)
        }
    }
}
